Create
準備將程式連接到 KINGSTAR 子系統。
語法
KsError Create(
int Instance,
int IdealProcessor
);
參數
Instance:在有多主站套件的情況下選擇想要使用的 KINGSTAR Runtime 實例。若沒有此套件,請將其設定為零 (0);若有此套件,則有效的實例應為 0 <= instance <= 63。此實例可依照您的需要而設,例如:您有三個實例並想使用第三個,則將之設為二(2)。
IdealProcessor:設定 KINGSTAR 子系統運作的核心。子系統的所有執行緒將在給定的處理器上運作。核心零固定分配給 Windows。您可根據您的設定將其他核心分配給子系統。預設情況下,IdealProcessor 設定為零,表示 KINGSTAR 將使用實例配置表中所配置的處理器(KINGSTAR 控制台 > Runtime 設定 > 一般設定)。若該表中缺少給定實例的設定,則子系統可在 RTX64 可用的任何核心上運作。若您將 IdealProcessor 設定為其他數字,例如二,則子系統將使用核心二。
回傳值
如果此函式執行成功,會回傳 errNoError
,否則會傳回錯誤碼。如需更多有關錯誤碼的資訊,請參閱 KsError 清單。
備註
- 此函式永遠需第一個呼叫,若 Create 未被呼叫,所有函式將回到
errWrongEnvironment
。 - 多實例代表有多個
ksRuntime.rtss
同時運行且控制不同的網路卡,若有 多主站套件,可使用數個 Create 來創建多個實例,例如,有三個 Create 函式,則這些函式可各自使用實例零、一和二;若無多主站套件,使用多個 Create 僅能創建一個實例,也就是實例零。多個應用程式可連結至一個KINGSTAR Runtime,但一個應用程式無法連到多個 Runtime。 - 一個 KINGSTAR Runtime = 一個
ksRuntime.rtss
。
範例
複製
//////////////////////////////////////////////////////////////////
//
// This code snippet demonstrates a basic flow of how to link to
// KINGSTAR subsystem and use the APIs to configure subsystem and
// control the EtherCAT network and motion devices.
//
//////////////////////////////////////////////////////////////////
KsError nRet = errNoError;
KsCommandStatus Command = { 0 };
SubsystemStatus Subsystem = { ecatOffline, ecatOffline, 0, 0, 0, {ecatOffline}, {ecatOffline}, {axisOffline} };
// Link to the KINGSTAR subsystem.
// If the KINGSTAR subsystem does not exist, it would create a new KINGSTAR subsystem.
// You have to call this API first before you use other KINGSTAR functions.
nRet = Create(0, 0);
// Check if the subsystem is started.
// A new KINGSTAR subsystem is not started by default.
nRet = GetStatus(&Subsystem, NULL);
if (Subsystem.State == ecatOP)
{
RtPrintf("Subsystem already started: %x\n", nRet);
}
else if (nRet == errNoError && Subsystem.State == ecatOffline)
{
// When the subsystem state is ecatOffline, you can use below APIs to configure settings:
//
// Subsystem Configuration APIs
// Axis Variable APIs
// AddModuleConfiguration()
// RemoveModuleConfiguration()
// SetConfigurationAxseCount()
// SetConfigurationIoCount()
//
// If you just want to use the default setting, skip this part and start the subsystem.
// Start the subsystem. It would change the subsystem state to ecatOP.
// This function is asynchronous so the network is not started yet when it returns.
// Use WaitForCommand() for synchronization.
Command = WaitForCommand(30, TRUE, Start());
}
// Now the subsystem state is ecatOP, you can use below APIs to control the subsystem and devices:
//
// Subsystem Control
// Slave Control
// Axis Control
// IO Control
// Heartbeat
// COM API
// Motion API
//
// You can also change the subsystem state or slave state.
// Please check "Usable EtherCAT states" section and the API pages to find out
// which functions you can use in different states.
// Stop the EtherCAT network before destroying KINGSTAR Subsystem or before re-configuration.
Command = WaitForCommand(5, FALSE, Stop());
if (Command.Error)
{
RtPrintf("Stop Failed: %d\n", Command.ErrorId);
}
// Terminates the KINGSTAR Subsystem if there is no other application connected to it.
nRet = Destroy();
if (nRet != errNoError)
{
RtPrintf("Destroy Failed: %x\n", nRet);
}
使用需求
RT | Win32 | |
---|---|---|
最低支援版本 | 4.0 | 4.0 |
標頭檔 | ksapi.h | ksapi.h |
程式庫 | KsApi_Rtss.lib | KsApi.lib |
參見